function load_dispatcher(url)
{
    selector = $("#selector").val();
	
    if(selector == undefined){
        selector = -1;
    }
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        selector:selector
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function load_add_assessment(id)
{
    var url = "curriculum/load_add_assessment";
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        id:id
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function load_manage_assessment()
{
    var url = "curriculum/load_manage_assessment";
    var subject, level, year;
	
    subject = $("#subject_selector").val();
    level = $("#level_selector").val();
    year = $("#yearterm_selector").val();
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        subject:subject,
        level:level,
        year:year
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function load_manage_SOW(order_by)
{
    var url = "curriculum/load_manage_SOW";
    var subject, level, year;
	
    subject = $("#subject_selector").val();
    level = $("#level_selector").val();
    year = $("#yearterm_selector").val();
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        subject:subject,
        level:level,
        year:year,
        order_by:order_by
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function delete_item(table, item)
{
    var url = "curriculum/delete_item";
    var selector = -1;
    // $("#the_content").html('');
    if ($("#selector").val() != undefined){
        selector = $("#selector").val();
    }
	
    $("#data_table").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        table:table,
        selector:selector,
        item:item
    }, function(data) {
        // $("#the_waiting").html('');
        $("#data_table").html(data).fadeIn();
    }, "html");
}

function add_item(table)
{
    var url = "curriculum/add_item";
    selector = $("#selector").val();
	
    $("#data_table").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        table:table,
        selector:selector
    }, function(data) {
        $("#data_table").html(data).fadeIn();
    }, "html");
}

function update_table(table)
{
    var url = "curriculum/update_table";
    //data
    var o = $("#myform").serializeArray();
    var selector = -1;
	
    if ($("#selector").val()  != undefined){
        selector = $("#selector").val();
    }
	
    o.push({
        "name":"table",
        "value":table
    });
    o.push({
        "name":"selector",
        "value":selector
    });
	
    var item_arr = $(".shown");
    var items = new Array();
	
    for(i = 0; i < item_arr.length; i++){
        o.push({
            "name":"shown[" + item_arr[i].value + "]",
            "value":(item_arr[i].checked?1:0)
        });
    }
	
    $("#data_table").html('<img src = "../images/spinner.gif" />');
    $.post(url, o, function(data) {
        $("#data_table").html(data).fadeIn();
    }, "html");
}

function printPartOfPage(elementId)
{
    var content = document.getElementById("printer_session").innerHTML;
    $("#printer_session").html('');
	
    var printContent = document.getElementById(elementId);
    var windowUrl = 'about:blank';
    var uniqueName = new Date();
    var windowName = 'Print' + uniqueName.getTime();
    var printWindow = window.open(windowUrl, windowName, 'left=50,top=50,width=500,height=500');

    printWindow.document.write(printContent.innerHTML);
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close();
	
    $("#printer_session").html(content);
	
}

function isUnsignedInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}
	
function trim(val) {
    var ret = val.replace(/^\s+/, '');
    ret = ret.replace(/\s+$/, '');

    return ret;
}

function add_assessment(id)
{
    var form_valid = true;
   
    //form_validations
    var name = trim($("#fullname").val());
    if(name == ""){
        form_valid = false;
    }
	
    if(!form_valid){
        $("#fullname").addClass("red");
        alert("Can't have empty name");
    } else {
        $("#fullname").removeClass("red");
    }
	
    if(!isUnsignedInteger($("#sequence").val())){
        $("#sequence").addClass("red");
        alert("Sequence must be positive number");
        form_valid = false;
    }else{
        $("#sequence").removeClass("red");
    }

    var o = $("#myform").serializeArray();
    o.push({
        "name":"id",
        "value":id
    });
	
    if(form_valid == true){
        var url = "curriculum/add_assessment";
		
        $("#the_content").html('');
        $("#the_waiting").html('<img src = "../images/spinner.gif" />');
        $.post(url, o, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");
    } else {
        $("#the_nothing").html('');
    }
}
function load_add_SLO(id)
{
    var url = "curriculum/load_add_SLO";
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        id:id
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function add_SLO(id){
    var form_valid = true;
    var o = $("#myform").serializeArray();
    
    if(!isUnsignedInteger($("#sequence").val())){        
        alert("Sequence must be positive number");
        form_valid = false;
    }else{
        $("#sequence").removeClass("red");
    }
    o.push({
        "name":"id",
        "value":id
    });
    if(form_valid == true){
        var url = "curriculum/add_SLO";		
        $("#the_content").html('');
        $("#the_waiting").html('<img src = "../images/spinner.gif" />');
        $.post(url, o, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();            
        }, "html");
    } else {		
        var url = "curriculum/load_add_SLO";
        $("#the_content").html('');
        $("#the_waiting").html('<img src = "../images/spinner.gif" />');
        $.post(url, o, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");	
    }
}

function subject_change()
{
    get_theme();
    get_topic();
    load_assessment_table();
}

function get_subject(){
    var url = "curriculum/get_subject";
    var yearterm_id = $("#year").val();
	
    $("#subject_selector").html('<img src = "images/spinner.gif" />');
    $.post(url, 
    {
        yearterm_id:yearterm_id
    }, 
    function(data) {
        if(data != 0){
            $("#subject_selector").html(data);
            get_subtheme();
        }
    }, "html");
}

function get_theme(){
    var url = "curriculum/get_theme";
    var subject_id = $("#subject_selector").val();
	
    $("#theme").html('<img src = "images/spinner.gif" />');
    $.post(url, 
    {
        subject_id:subject_id
    }, 
    function(data) {
        if(data != 0){
            $("#theme").html(data);
            get_subtheme();
        }else{
            alert(data);
        }
    }, "html");
}

function get_subtheme(){
    var url = "curriculum/get_subtheme";
    var theme_id  = $("#theme_selector").val();
	
    $("#subtheme").html('<img src = "images/spinner.gif" />');
    $.post(url, {
        theme_id:theme_id
    }, function(data) {
        // alert(data);
        if(data != 0){
            $("#subtheme").html(data);
        }else{
            alert(data);
        }
    }, "html");
}

function get_topic(){
    var url = "curriculum/get_topic";
    var subject_id = $("#subject_selector").val();
	
    $("#topic").html('<img src = "images/spinner.gif" />');
    $.post(url, 
    {
        subject_id:subject_id
    }, 
    function(data) {
        if(data != 0){
            $("#topic").html(data);
            get_subtopic();
        }else{
            alert(data);
        }
    }, "html");
}

function get_subtopic(){
    var url = "curriculum/get_subtopic";
    var topic_id = $("#area_selector").val();
	
    $("#subtopic").html('<img src = "images/spinner.gif" />');
    $.post(url, {
        topic_id:topic_id
    }, function(data) {
        if(data != 0){
            $("#subtopic").html(data);
        }else{
            alert(data);
        }
    }, "html");
}
function load_assessment_table()
{
    var url = "curriculum/get_assessment";
    var subject_id = $("#subject_selector").val();
    var level_id = $("#level_selector").val();
    var yearterm_id = $("#yearterm_selector").val();
	
    $("#assessment").html('<img src = "images/spinner.gif" />');
    $.post(url, {
        subject_id:subject_id,
        level_id:level_id,
        yearterm_id:yearterm_id,
        mode:0
    }, function(data) {
        if(data != 0){
            $("#assessment").html(data);
        }else{
            alert(data);
        }
    }, "html");
	
    $("#assessment_cell").html('<img src = "images/spinner.gif" />');
    $.post(url, {
        subject_id:subject_id,
        level_id:level_id,
        yearterm_id:yearterm_id,
        mode:1
    }, function(data) {
        if(data != 0){
            $("#assessment_cell").html(data);
        }else{
            alert(data);
        }
    }, "html");
}


function update_url(id)
{
    var form_valid = true;
    //form_validations
    
    var o = $("#myform").serializeArray();
    var filePaths=$("#filePaths").val();
    o.push({
        "name":"id",
        "value":id,
        "filePaths":filePaths
    });
	
    if(form_valid == true){
        var url = "curriculum/update_url";
        $("#the_content").html('');
        $("#the_waiting").html('<img src = "../images/spinner.gif" />');
        $.post(url, o, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");
    }
}


function add_SOW(id)
{
    var form_valid = true;
    //form_validations
    if ($("#week").val()  != undefined){
        if(!isUnsignedInteger($("#week").val())){
            $("#week").addClass("red");
            alert("Week must be positive number");
            form_valid = false;
        }else{
            $("#sequence").removeClass("red");
        }
    } 
	
    var o = $("#myform").serializeArray();
    var filePaths=$("#filePaths").val();
    o.push({
        "name":"id",
        "value":id,
        "filePaths":filePaths
    });


	
    if(form_valid == true){
        var url = "curriculum/add_SOW";
		
        $("#the_content").html('');
        $("#the_waiting").html('<img src = "../images/spinner.gif" />');
        $.post(url, o, function(data) {
            $("#the_waiting").html('');
            $("#the_content").html(data).fadeIn();
        }, "html");
    } else {
        $("#the_nothing").html('');
    }
}

function load_SOW(id, lock_upload)
{
    var url = "curriculum/load_add_SOW";
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        id:id,
		lock_upload:lock_upload
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}
function retrieve(){
    var sub = $("#subject").val();
    var lev = $("#level").val();
    var yearterm = $("#yearterm").val();        
    if(sub == 0 || lev == 0 || yearterm == 0){
        alert('Please pick 3 options');
        return;
    }
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/curriculum/load_manage_SLO',
        data: "subject_id="+sub+"&level_id="+lev+"&yearterm="+yearterm,
        success: function(data){
            $('#the_content').html(data);
        }
    })	
}

function delete_sum_lo(id){	
    var url = SITE_URL+"/curriculum/del_SLO";
    $.post(url, {
        id:id
    }, function(data) {
        if(data == 0){
            var the_id = "#lo_"+id;
            $(the_id).fadeOut();
        }else alert("try again OR refresh and try again");	
    }, "html");	 
}

function add_sum_lo(){
    //	var sub = $("#subject").val();
    //	var lev = $("#level").val();
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/curriculum/load_add_SLO',
        data: "",
        success: function(data){
            $('#the_content').html(data);
        }
    })		
}

